﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraPrinting;
using DevExpress.XtraEditors;
using DevExpress.LookAndFeel;
using System.Drawing;
using DevExpress.XtraSplashScreen;


public class FormGlobals
{
    public static SplashScreenManager splashScreenManager = new SplashScreenManager(Application.OpenForms[0], typeof(WaitForm1), true, true);

    public static void ShowWaitForm()
    {
        if (!splashScreenManager.IsSplashFormVisible)
        {
            splashScreenManager.ShowWaitForm();
        }
    }
    public static void CloseWaitForm()
    {
        if (splashScreenManager.IsSplashFormVisible)
        {
            splashScreenManager.CloseWaitForm();
            splashScreenManager.Dispose();
        }
    }

    public static void ExportDataToExcel(GridControl gc, string sheetName)
    {
        SaveFileDialog f = new SaveFileDialog();
        f.Filter = "Excel file (*.xls)|*.xls";
        f.FileName = sheetName;
        if (f.ShowDialog() == DialogResult.OK)
        {
            string fileName = f.FileName;
            ExportDataToExcelWithSheetName(gc, fileName, sheetName);

            try
            {
                System.Diagnostics.Process.Start(fileName);
            }
            catch (Exception ex) { throw new Exception(ex.Message); }

        }

    }

    public static void ExportDataToExcelWithSheetName(GridControl gc, string fileName, string sheetName)
    {
        DevExpress.XtraPrinting.XlsExportOptions option = new DevExpress.XtraPrinting.XlsExportOptions();
        option.SheetName = sheetName;
        gc.ExportToXls(fileName, option);
    }

    public static void GridView_AddColumn(GridView gv, GridColumn gc, string FieldName, string Caption, string Format, int Width, bool Visible, bool ReadOnly)
    {
        gc.FieldName = FieldName;
        gc.Caption = Caption;
        if (Width > 0)
            gc.Width = Width;
        gc.Visible = Visible;
        gc.OptionsColumn.ReadOnly = ReadOnly;
        if (Format.Length > 1)
        {
            gc.DisplayFormat.FormatString = Format;
            gc.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
        }
        gv.Columns.Add(gc);

        if (gv.Columns.Count == 1)
        {
            //Hide row header GroupPanel
            //this.gridView1.OptionsView.ShowGroupPanel = false;
            //Hide row header
            gv.OptionsView.ShowIndicator = false;

            //Hide column header
            //this.gridView1.OptionsView.ShowColumnHeaders = false;

            //Set selection mode to fullrow
            gv.OptionsSelection.EnableAppearanceFocusedCell = false;

            gv.OptionsView.ColumnAutoWidth = false;
            gv.OptionsBehavior.Editable = false;
            gv.RowCellStyle += gridView1_RowCellStyle;
        }
    }

    static void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
    {
        GridView View = sender as GridView;
        //doi mau row chan
        if (e.RowHandle >= 0)
        {
            if (e.RowHandle % 2 == 0)
            {
                //e.Appearance.BackColor = Color.FromArgb(240, 240, 240);//Color.DarkCyan;
            }
        }
    }
    public static DialogResult MessageBoxShow(UserLookAndFeel lookAndFeel, IWin32Window owner, string text, string caption, DialogResult[] buttons, Icon icon, int defaultButton, MessageBoxIcon messageBeepSound)
    {
        //MessageBeep((int)messageBeepSound);
        XtraMessageBoxForm form = new XtraMessageBoxForm();
        return form.ShowMessageBoxDialog(new XtraMessageBoxArgs(lookAndFeel, owner, text, caption, buttons, icon, defaultButton));
    }
    public static DialogResult Message_Information(string text, string caption)
    {
        UserLookAndFeel lookAndFeel = UserLookAndFeel.Default;
        IWin32Window owner = Application.OpenForms[0];
        return MessageBoxShow(lookAndFeel, owner, text, caption, new DialogResult[] { DialogResult.OK }, System.Drawing.SystemIcons.Information, 1, MessageBoxIcon.Information);
    }
    public static DialogResult Message_QuestionYesNo(string text, string caption)
    {
        UserLookAndFeel lookAndFeel = UserLookAndFeel.Default;
        IWin32Window owner = Application.OpenForms[0];
        return MessageBoxShow(lookAndFeel, owner, text, caption, new DialogResult[] { DialogResult.Yes, DialogResult.No }, System.Drawing.SystemIcons.Question, 2, MessageBoxIcon.Question);
    }
    public static DialogResult Message_OKCancel(string text, string caption)
    {
        UserLookAndFeel lookAndFeel = UserLookAndFeel.Default;
        IWin32Window owner = Application.OpenForms[0];
        return MessageBoxShow(lookAndFeel, owner, text, caption, new DialogResult[] { DialogResult.OK, DialogResult.Cancel }, System.Drawing.SystemIcons.Application, 2, MessageBoxIcon.None);
    }
    public static DialogResult Message_Warning(string text, string caption)
    {
        UserLookAndFeel lookAndFeel = UserLookAndFeel.Default;
        IWin32Window owner = Application.OpenForms[0];
        return MessageBoxShow(lookAndFeel, owner, text, caption, new DialogResult[] { DialogResult.OK }, System.Drawing.SystemIcons.Warning, 1, MessageBoxIcon.Warning);
    }
    public static DialogResult Message_Error(string text, string caption)
    {
        UserLookAndFeel lookAndFeel = UserLookAndFeel.Default;
        IWin32Window owner = Application.OpenForms[0];
        return MessageBoxShow(lookAndFeel, owner, text, caption, new DialogResult[] { DialogResult.OK }, System.Drawing.SystemIcons.Error, 1, MessageBoxIcon.Error);
    }
}
